package com.ckd.flyingtrip.shared.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.Alipay;
import com.ckd.flyingtrip.activity.BaseActivity;
import com.ckd.flyingtrip.activity.HomePayActivity;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.dialog.PayWayDialog;
import com.ckd.flyingtrip.shared.bean.SharedCarInfoBean;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SharedCarPayActivity extends BaseActivity {

    @BindView(R.id.toolber_color_back)
    ImageView back;
    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.activity_sharedcar_pay_time)
    TextView time;
    @BindView(R.id.activity_sharedcar_pay_distance)
    TextView distance;
    @BindView(R.id.activity_sharedcar_pay_speed)
    TextView speed;
    @BindView(R.id.activity_sharedcar_pay_money)
    TextView money;

    private PayWayDialog.Builder pb;

    private boolean aBoolean = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharedcar_pay);
        ButterKnife.bind(this);

        initView();
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.GET, Constants.shared_info, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("共享用户信息", s);
                SharedCarInfoBean bean = new Gson().fromJson(s, SharedCarInfoBean.class);
                dismisProgressDialog();
                if (bean.getCode().equals("200")) {
                    if (bean.getData().getProcessTrip() != null) {
                        time.setText(mm2hsm(Long.parseLong(bean.getData().getProcessTrip().getTripTimes())));
                        distance.setText(bean.getData().getProcessTrip().getTripMileage());
                        speed.setText(String.format("%.2f",
                                Double.parseDouble(bean.getData().getProcessTrip().getTripMileage()) / Double.parseDouble(bean.getData().getProcessTrip().getTripTimes())));
                        money.setText(bean.getData().getProcessTrip().getRealPrice());
                    } else {
                        Tools.toast("支付完成！");
                        finish();
                    }
                } else {
                    Tools.toast(bean.getMessage());
                    finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> mHeader = new HashMap<String, String>();
                mHeader.put("TOKEN_XCX_SHARE", SharedCarActivity.token);
                return mHeader;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    private void initView() {
        title.setText("正在用车");
        back.setVisibility(View.GONE);
    }

    @OnClick({R.id.toolber_color_back, R.id.activity_sharedcar_pay_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                finish();
                break;
            case R.id.activity_sharedcar_pay_btn:
                pb = new PayWayDialog.Builder(SharedCarPayActivity.this,
                        new PayWayDialog.Builder.PriorityListener() {
                            @Override
                            public void refreshPriorityUI(String f) {
                                volleyQueryFei(SharedCarActivity.token, f);
                                pb.create().dismiss();
                            }
                        });
                pb.create().show();
                break;
        }
    }

    private void volleyQueryFei(String token, String type) {
        if (type.equals("weixin")) {
//            Weixin weixin = new Weixin(GoumaiActivity.this, "");
//            weixin.volleyDINGDAN(id);
            dismisProgressDialog();
        } else if (type.equals("alipay")) {
            createLoadingDialog(this, "正在支付中...", true);
            Alipay alipay = new Alipay(SharedCarPayActivity.this, "");
            alipay.volleyQueryShared(token);
        }
    }

    private String mm2hsm(long mmTime) {
        int h = (int) (mmTime / (60 * 60));
        int min = (int) (mmTime % (60 * 60) / (60));
        int sec = (int) ((mmTime % (60 * 60) % (60)));
        Formatter formatter = new Formatter();
        String result = formatter.format("%02d:%02d:%02d", h, min, sec).toString();
        return result;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
